#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <queue>
using namespace std;

struct node
{
	long long chang;
	long long shu;
}a[2010];

bool cmp(node A, node B)
{
	return A.chang < B.chang;
}

void solve()
{
	int n;
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
		cin >> a[i].chang >> a[i].shu;
		if (a[i].shu > 3)
			a[i].shu = 3;
	}
	sort(a + 1, a + n + 1, cmp);
	long long ans = 0;
	for (int i = 1; i <= n; i++)
	{
		if (a[i].shu == 3)
			ans++;
	}
	for (int i = 1; i <= n; i++)
	{
		if (a[i].shu >= 2)
		{
			int j = i;
			for (j = i + 1; j <= n; j++)
			{
				if (a[j].chang >= 2 * a[i].chang)
					break;
			}
			j -= 2;
			ans += j;
		}
	}
	for (int i = 3; i <= n; i++)
	{
		int k = 1;
		for (int j = i - 1; j >= 1; j--)
		{
			while (k < j && a[k].chang + a[j].chang <= a[i].chang)
			{
				k++;
			}
			if (k < j)
			{
				ans += j - k;
			}
		}
	}
	cout << ans << endl;
	return;
}

int main()
{
	int t;
	cin >> t;
	while (t--)
		solve();
	return 0;
}